package tq.com.tqstudent.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lib.common.utils.TimeUtil;

import java.util.ArrayList;
import java.util.List;

import tq.com.tqstudent.App;
import tq.com.tqstudent.R;
import tq.com.tqstudent.bean.MalistBean;
import tq.com.tqstudent.bean.RecodeBean;
import tq.com.tqstudent.ui.activity.classes.VideoPlayActivity;
import tq.com.tqstudent.widgets.PreviewImgActivity;

/**
 * @Author rames
 * @date 2019/3/27 10:44
 */
public class RecodeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<RecodeBean> list;//数据源
    private Context context;//上下文

    public RecodeAdapter(Context context, List<RecodeBean> list) {
        this.context = context;
        setData(list);
    }

    public void setData(List<RecodeBean> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }

    public void addData(List<RecodeBean> list) {
        this.list.addAll(list);
        this.notifyDataSetChanged();
    }

    public List<RecodeBean> getDatas(){
        return list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //选择类型
        switch (viewType) {
            // 标题
            case RecodeBean.TYPE_TITLE:
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_title_recode, parent, false);
                return new TitleViewHolder(view);

            // 内容
            case RecodeBean.TYPE_CONTENT:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_content_recode, parent, false);
                return new ContentViewHolder(view);

            // 时间
            case RecodeBean.TYPE_TIME:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_time_recode, parent, false);
                return new TimeViewHolder(view);

            // 图片
            case RecodeBean.TYPE_IMAGE:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_image_recode, parent, false);
                return new ImageViewHolder(view);
        }
        return null;
    }

    //绑定
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        RecodeBean item = list.get(position);
        // title
        if (holder instanceof TitleViewHolder) {
            TitleViewHolder view = ((TitleViewHolder) holder);
            view.contentTV.setText(item.meterial_title);

            // 内容
        } else if (holder instanceof ContentViewHolder) {
            ContentViewHolder view = ((ContentViewHolder) holder);
            view.contentTV.setText(item.meterial_title);
            view.contentTV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    VideoPlayActivity.show((Activity) context, item.material_id, item.attachment_id, item.meterial_title, App.getFileUrl(item.content), item.material_id);
                }
            });

            // 时间
        } else if (holder instanceof TimeViewHolder) {
            TimeViewHolder view = ((TimeViewHolder) holder);
            view.timeIV.setText(item.create_time);
//            view.timeIV.setText(item.create_time + " 累计学习："+ TimeUtil.getTimeDistance(Integer.parseInt(item.watch_sec)));

            // 图片
        } else if (holder instanceof ImageViewHolder) {
            ImageViewHolder view = ((ImageViewHolder) holder);
            view.indexTV.setText("共查看了" + item.view_cnt + "次");
            view.line.setVisibility(View.GONE);
            Glide.with(context).load(App.getFileUrl(item.content_cover)).into(view.image);

            view.image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ArrayList<String> urls = new ArrayList<>();
                    urls.add(App.getFileUrl(item.content_cover));
                    PreviewImgActivity.show((Activity) context, urls, 0, item.material_id);
                }
            });

            view.playLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    VideoPlayActivity.show((Activity) context, item.material_id, item.attachment_id, item.meterial_title, App.getFileUrl(item.content), item.material_id);
                }
            });
        }
    }

    //有多少个item？
    @Override
    public int getItemCount() {
        return null == list ? 0 : list.size();
    }

    //item类型
    @Override
    public int getItemViewType(int position) {
        return list.get(position).type;
    }

//=======================以下为item中的button控件点击事件处理===================================

    //第一步：自定义一个回调接口来实现Click和LongClick事件
    public interface OnItemClickListener {
        void onItemClick(View v, int position);

        void onItemLongClick(View v);
    }

    public RecodeAdapter.OnItemClickListener mOnItemClickListener;//第二步：声明自定义的接口

    //第三步：定义方法并暴露给外面的调用者
    public void setOnItemClickListener(RecodeAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    //第一个item类型
    class TitleViewHolder extends RecyclerView.ViewHolder {
        private TextView contentTV;

        public TitleViewHolder(View itemView) {
            super(itemView);
            contentTV = itemView.findViewById(R.id.content_tv);
        }
    }

    //第二个内容item类型
    class ContentViewHolder extends RecyclerView.ViewHolder {
        private TextView contentTV;

        public ContentViewHolder(View itemView) {
            super(itemView);
            contentTV = itemView.findViewById(R.id.content_tv);
        }
    }

    //第三个time item类型
    class TimeViewHolder extends RecyclerView.ViewHolder{
        private TextView timeIV;

        public TimeViewHolder(View itemView) {
            super(itemView);
            timeIV = itemView.findViewById(R.id.time_tv);
        }
    }

    //第四个图片item类型
    class ImageViewHolder extends RecyclerView.ViewHolder {
        private TextView indexTV;
        private ImageView image;
        private LinearLayout playLayout;
        private View line;

        public ImageViewHolder(View itemView) {
            super(itemView);
            playLayout = itemView.findViewById(R.id.play_layout);
            indexTV = itemView.findViewById(R.id.index_tv);
            image = itemView.findViewById(R.id.image);
            line =  itemView.findViewById(R.id.line);
        }
    }
}
